package com.example.baseactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<String> data ;
    List<String> dataBAk;
    String search ="" ;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*3.造数据*/
        String x = "老番茄,某幻君,马了顶大,中国BOY大猩猩,崔西 ";
        String[] xs = x.split(","); // 用split方法切割成数组
        List<String> xl = Arrays.asList(xs); //转变成一个数组
        data =new ArrayList<>(xl);
        dataBAk =data ;


        /*1.实例化*/
        ListView lv = findViewById(R.id.lv);
        myAdapter= new MyAdapter() ;

        lv.setAdapter(myAdapter);

    }


    /* 2.BaseAdapter   */
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override        //int 第几行   View 视图   ViewGroup
        public View getView(int position, View view, ViewGroup parent) {
            if (view == null) {
                //制作view
                view =getLayoutInflater().inflate(R.layout.cell,null);
            }

            /*  把两个TextView取出来 进行实例化 */
            TextView tl = view.findViewById(R.id.title);
            TextView da = view.findViewById(R.id.date);

            String s=data.get(position);




            tl.setText(s);
            tl.setTextColor(Color.parseColor("#BC8F8F"));
            da.setText("2020年-12月-15日");
            da.setTextColor(Color.parseColor("#63B8FF"));

            return view;

        }

        public void  beforeTextChanged(CharSequence charSequence ,int i ,int i1 ,int i2  ){

        }
                     //筛选 功能
        public  void  onTextChanged(CharSequence charSequence,  int i,int i1 ,int i2){
            if (charSequence.length()==0 ){


                data= dataBAk ;
            }else {
                data =new ArrayList<>() ; 
                for (String txt :dataBAk){
                    if (txt.contains(charSequence)){
                        data.add(txt) ;
                    }
                }
            }
            myAdapter.notifyDataSetChanged(); /*  刷新 重新绘制视图*/

        }


    }

}

